package com.hzyb.oldpack.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.apache.tomcat.util.http.fileupload.RequestContext;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.hzyb.oldpack.dao.HzybDao;
import com.hzyb.oldpack.model.ShopCommodity;
import com.hzyb.oldpack.service.HzybService;
@Service
public class HzybServiceImpl implements HzybService{
	
	@Value("${hzyb.imagesPath}")
	private String mImagesPath;
	//图片虚拟根目录
	@Value("${picture.virtual.rootPath}")
	private String pictureVirtualRootPath;
	@Resource
	private HzybDao HzybDao;

	@Override
	public List<ShopCommodity> findall() {
		return HzybDao.findAll();
	}

	@Override
	public String  upFile(@RequestParam("file") MultipartFile myfile,HttpServletRequest request, HttpServletResponse resp) throws Exception {
		 //file为获取到的前台的文件，每个框架获取的方式不一样，可按需去查询
        File file = (File) myfile ;

        //fileName为文件的名称，可通过file获取，不同框架获取的方式不一样
        String fileName = file.getName();

        //获取文件的项目的根路径，不同框架获取的方式不一样，这里使用struts2的获取方式
        String basePath=mImagesPath+"";

        //拼接成完整的指定的文件路径名，创建新文件
        String filePath = basePath+File.separator+fileName;
        File file1 = new File(filePath);
        if(file1.exists()){
            file1.delete();
        }

        //使用输入流读取前台的file文件              
        InputStream is=new FileInputStream(file);  

        //循环读取输入流文件内容，通过输出流将内容写入新文件
        OutputStream os=new FileOutputStream(file1);  
        byte buffer[]=new byte[1024];  
        int cnt=0;  
        while((cnt=is.read(buffer))>0){  
            os.write(buffer, 0, cnt);  
        }  
        //关闭输入输出流
        os.close();
        is.close();  
        return  "";
	}

	@Override
	public String getUUID() {
		String uuid = UUID.randomUUID().toString().replaceAll("-", "");
		return uuid;
	}

	
	

	

	

	

}
